package com.gzyj.flink.gps.sink.gp;

import com.gzyj.flink.gps.factory.MybatisSessionFactory;
import com.gzyj.flink.gps.mapper.tidb.BaobiaoAlarmsummaryMapper;
import com.gzyj.flink.pojo.gps.BaobiaoAlarmsummary;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class BaobiaoAlarmsummarySink extends RichSinkFunction<BaobiaoAlarmsummary> {

    SqlSessionFactory sqlSessionFactory;

    @Override
    public void open(Configuration parameters) throws Exception {
        sqlSessionFactory = MybatisSessionFactory.getSqlSessionFactory();
    }

    @Override
    public void close() throws Exception {

    }

    @Override
    public void invoke(BaobiaoAlarmsummary value, Context context) throws Exception {

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        try {

            BaobiaoAlarmsummaryMapper alarmsummaryMapper = sqlSession.getMapper(BaobiaoAlarmsummaryMapper.class);

            alarmsummaryMapper.insertSelective(value);

            sqlSession.commit();


        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

    }

}
